from datetime import timedelta
from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm

from app.config import get_settings
from app.logger import log
from app.schemas.auth import TokenSchema
from app.services.auth import authenticate_user, create_access_token

ACCESS_TOKEN_EXPIRE_DAYS = get_settings().access_token_expire_days


router = APIRouter()


@router.post("/", response_model=TokenSchema)
async def login_for_access_token(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
    """Login form"""
    log.info(f"Login form received: {form_data}")
    user = await authenticate_user(form_data.username, form_data.password)
    if user is None:
        log.warning(f"User with email {form_data.username} not found or password is incorrect")
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(days=ACCESS_TOKEN_EXPIRE_DAYS)
    access_token = create_access_token(data={"sub": user.email}, expires_delta=access_token_expires)
    return {"access_token": access_token, "token_type": "bearer"}
